package com.schx.budget.controller;


import com.schx.budget.pojo.Account;
import com.schx.budget.pojo.EditAccount;
import com.schx.budget.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Classname FileService
 * @Description TODO
 * @Date 2021/2/3 14:50
 * @Created by cyh
 */
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/subLogin")
    public String subLogin(@RequestBody Account account){
        //参数校验
        if(account==null||account.getOrgid()==null||account.getPassword()==null||"".equals(account.getPassword())){
            return  "{\"code\":"+201+" ,\"data\":\"帐号或密码有误！\"}";
        }
        //认证
        String token = loginService.doLogin(account);

        //颁发令牌
        return "{\"code\":"+200+" ,\"data\":\""+token+"\"}";
    }


    @PostMapping("/edit/password")
    public String editPassword(@RequestBody EditAccount account){
        if(account==null||account.getPassword()==null||"".equals(account)){
            return "{\"code\":"+500+" ,\"data\":\"验证失败!\"}";
        }
        if(account.getNewPassword()==null||"".equals(account.getNewPassword())){
            return "{\"code\":"+500+" ,\"data\":\"新密码不能为空!\"}";
        }
        if(account.getNewPassword().equals(account.getPassword())){
            return "{\"code\":"+500+" ,\"data\":\"新密码与旧密码相同!\"}";
        }

        return loginService.editPassword(account);
    }



    @GetMapping("/list/org")
    public List<Account> listOrg(){
        return loginService.listOrg();
    }

}
